<template>
  <div class="department-contioner">
    <el-tree :data="depts" :props="defaultProps" :expand-on-click-node="false" default-expand-all>
      <template v-slot="{data}">
        <el-row style="width: 100%;">
          <el-col :span="20"><div>{{ data.name }}</div></el-col>
          <el-col :span="4">
            <div class="rigih-ele">
              <span class="re-name">{{ data.managerName }}</span>
              <!-- 下拉菜单 -->
              <el-dropdown @command="handleCommand($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="delete">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </el-col>
        </el-row>
      </template>
    </el-tree>

    <!-- 导入封装的弹窗组件 -->
    <AddEditDiomo
      ref="GetCompanyREF"
      :isedit.sync="isedit"
      :dialog-form-visible.sync="dialogFormVisible"
      :department-list="departmentList"
      :curret-id="curretId"
      @add-success="companyDepartment"
    />
  </div>
</template>

<script>
// 导入接口
import { companyDepartmentRequest, deleteCompanyRequest } from '@/api/department.js'
// 导入封装的递归函数
import { getsome } from '@/utils'
// 导入封装好的组件
import AddEditDiomo from './components/addEditDiomo.vue'
export default {
  name: 'Department',
  components: {
    AddEditDiomo
  },
  data() {
    return {
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 这是封装子组件的值,父子传值传到组件(显示对话框的)
      dialogFormVisible: false,
      // 这是要传递到子组件的数据(进行校验的)
      departmentList: [],
      // 这是要传递到子组件的数据
      curretId: '',
      // 这是要传递到子组件判断的变量
      isedit: false
    }
  },
  created() {
    // 调用获取部门列表函数
    this.companyDepartment()
  },
  methods: {
    // 封装发送获取部门列表函数
    async  companyDepartment() {
      const data = await companyDepartmentRequest()
      //  这是树状数据
      this.depts = getsome(data.data, 0)
      // 这是要传递给子组件的数据
      this.departmentList = data.data
    },
    // 利用elm下拉菜单内置方法,优化绑定事件
    handleCommand(type, id) {
      this.curretId = id
      if (type === 'add') {
        this.isedit = false
        // 弹窗弹出
        this.dialogFormVisible = true
      } else if (type === 'delete') {
        // 删除按钮
        this.$confirm('确认删除吗, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          // 发生删除请求
          await deleteCompanyRequest(id)
          this.companyDepartment()
          // 渲染
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      } else {
        // 编辑
        this.isedit = true
        this.dialogFormVisible = true
        // 使用ref调用子组件的函数方法
        // this.$nextTick(() => {
        //   this.$refs.GetCompanyREF.GetCompany()
        //   // console.log(2)
        // })
      }
    }
  }

}
</script>

<style lang="scss" scoped>
 .department-contioner{
  padding: 30px 140px;
  .rigih-ele{
    .re-name{
      margin-right: 10px;
    }
  }
 }
</style>
